<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	
	public $WEB_SET;
	function __construct()
     {
     	
     	parent::__construct();

     	//按当前语言载入语言文件     	
     	if($this->session->userdata("lang")=="")
     	{
			$this->lang->load('info',$this->config->item("language")); 
		}else
		{
			$this->lang->load('info',$this->session->userdata("lang")); 
		}
		
		//加载网站设置
		$this->load->model("Agent_model");
		$this->WEB_SET=$this->Agent_model->load_websetinfo();
		
		//记录日志
		$this->load->model("Userlog_model");
		$this->Userlog_model->write_userlog();
     }
	//注册
	public function register()
	{
		$data = array();
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-register';
	   	
	   		
		$this->load->view('user/register',$data);
	}
	//登陆
	public function login()
	{
		
		$data = array();
	   	
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-login';
	   	
	   	$this->load->model("Setting_model");
		$homeadv=$this->Setting_model->get_advContext("adv2",$this->WEB_SET["WEB_AgentCode"]);
	   	$data['loginadv'] ="<img src=\"".site_url($homeadv->Context)."\" />";	//广告信息
	   	
	   	$data['returnurl']=$this->input->get("returnurl");
	   	$data['error_msg']=$this->uri->segment(3);
		$this->load->view('user/login',$data);
		
	}
	//处理注册
	public function doregister()
	{
		$this->load->model('User_model');
		$regresult=$this->User_model->registeruser($_POST,$this->WEB_SET);
		echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>';
		if($regresult[0])
		{
			
			//echo "注册成功".$regresult[0];
			
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"Registered successfully\");";
			echo "window.location='".base_url()."'";
			echo "</SCRIPT>";
			
		}else
		{
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"$regresult[1]\");";
			echo "history.back(-1);";
			echo "</SCRIPT>";
		}
	}
	
	//处理登陆
	public function dologin()
	{
		$this->_check_yzm1();

        	$name = $this->input->post('useraccount');
		$password = $this->input->post('userpassword');	
		$agentcode=$this->session->userdata("WEB_AgentCode");
		
		if($this->session->userdata("page_returnurl")!="")
		{
			$returnurl= $this->session->userdata("page_returnurl");
		}else
		{
			$returnurl= $this->input->post('returnurl');	
		}
		
		
		$this->load->model('User_model');
		$_customer = $this->User_model->check_customer($name,$password,$agentcode);
		//print_r($_customer);
		
		if ($_customer){
			
			$customer = array(
				   'user_name'  => $_customer['username'],
				   'user_account'  => $_customer['useraccount'],
				   'user_in' => TRUE,
				   'user_last_login' => $_customer['last_login']
			   );
			$this->session->set_userdata($customer);
            
			$this->User_model->update_last_login($_customer['useraccount']);
			
			if($returnurl!="")
			{
				//返回登录前的页面
				
				$LastPost=$this->session->userdata("page_LastPost");
				$data=array();
				$data["LastPost"]=$LastPost;
				
				
				
				$data["doaction"]=$returnurl;
				//print_r($LastPost);
				$this->load->view("ajax/dolastpost",$data);
				//redirect($returnurl);
				
			}else
			{
				redirect(site_url("home"));
			}

			
		}else{
		
            		echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>';
				echo "<SCRIPT LANGUAGE=\"JavaScript\">";
				echo "alert(\"".lang("loginfail")."\");";
				echo "window.location='".site_url("user/login")."'";
				echo "</SCRIPT>";
		}
	}
	//登出
	public function logout()
	{
		$this->session->sess_destroy();
		redirect('home');
	}
	
	//检查用户名是否重复
	public function check_username()
	{
		$qs = query_string_to_array();

	    	$name = $qs['txuseraccount'];
		$this->load->model('User_model');
		$msg = array('Result' => true);
		if ($name){
		    if ($this->User_model->check_name($name)){
				$msg = array('Result' => false);
			}				
		}
		echo json_encode($msg);
	}
	//生成验证码
	public function yzm_img1()
	{
		$this->load->helper('captcha1');
		code();
	}
	
	/**
	 * 检查登陆验证码是否准确
	 *
	 *
	 */	
	function _check_yzm1()
	{
		session_start();
        	$Verifier = $this->input->post('tbxLoginVerifier');
        	if (!empty($_SESSION['verify1']) && $_SESSION['verify1'] == $Verifier)
        	{

	   	}else
	   	{
			redirect('user/login/verifier1');
	   	}
		return;
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */